package com.atguigu.hdfs.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;

import java.net.URI;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2021/11/12 17:23
 * @description:
 */
@Slf4j
public class HDFSClientUtil {

    private HDFSClientUtil() {
    }


    /**
     * @return hdfs client
     */
    public static FileSystem getHDFSClientInstance(String nameNodeUrl, String user) {

        FileSystem fsOp = null;

        try {
            URI uri = new URI(nameNodeUrl);
            fsOp = FileSystem.get(uri, new Configuration(), user);

        } catch (Exception e) {
            log.info("[**{}**]", e.getMessage());
        }

        return fsOp;
    }


    /**
     *
     * @param nameNodeUrl NameNodeURL
     * @param user 用户
     * @param conf 自定义Hadoop集群配置
     * @return
     */
    public static FileSystem getHDFSInstanceByDef(String nameNodeUrl, String user, Configuration conf) {

        FileSystem fsOp = null;

        try {
            URI uri = new URI(nameNodeUrl);
            fsOp = FileSystem.get(uri, conf, user);

        } catch (Exception e) {
            log.info("[**{}**]", e.getMessage());
        }

        return fsOp;
    }
}

